package com.home.demo.controller;

import com.home.demo.common.PageUtils;
import com.home.demo.common.R;
import com.home.demo.entity.dto.role.RoleInfoDTO;
import com.home.demo.entity.vo.roleAndMenu.RoleAndMenuVO;
import com.home.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * 角色Controller
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = roleService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 角色下拉框
     * @return
     */
    @GetMapping("getRoleList")
    public R getRoleList(){
        return R.ok().put("data",roleService.list());
    }

    @GetMapping("getRoleSelect")
    public R getRoleSelect(){
        return R.ok().put("data",roleService.list());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		RoleAndMenuVO role = roleService.getByIdInfo(id);

        return R.ok().put("data", role);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RoleInfoDTO role){
		roleService.saveInfo(role);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RoleInfoDTO role){
		roleService.updateInfoById(role);

        return R.ok();
    }

    /**
     * 删除角色的时候顺带删除角色与用户与菜单关联的信息
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		roleService.removeByIdsInfo(Arrays.asList(ids));

        return R.ok();
    }

}
